<?php

class Db_Manipulation{
	
	var $db;	
	
	function _construct(){
		$host = "localhost";
		$dbUser = "root";
		$dbPass = "";
		$dbName = "taichinh";
		$this->db = new DB($dbName,$host,$dbUser,$dbPass);			
	}
	
	//LẤY TOÀN BỘ DATA TỪ CSDL
	function getAll($fields,$tbName,$limit=null){		
		if(is_array($fields))
			$fields = implode(',',$fields);		
		$res = $this->db->query("SELECT $fields FROM `$tbName` $limit");
		$arr = array();
		$i=0;
		while ($line = $this->db->fetchNextObject($res)) {
			//var_dump($line);
			//array_push($arr,$line);
			$arr[$i] = $line;
			//use($line->MaCongTy);
			$i++;
		}
		//var_dump($arr);
		return $arr	;
	}
	
	
	//LẤY NHIỀU FIELD VỚI ĐIỀU KIỆN
	function GetManyFields_Condition($fields,$tbName,$condition){
		$line = $this->db->queryUniqueObject("SELECT $fields FROM $tbName WHERE $condition");
		//var_dump($line);
		return $line;
	}
	
	//LẤY 1 CỘT VỚI ĐIỀU KIỆN
	function getOneField_Condition($field,$tbName,$condition){
		$field = $this->db->queryUniqueValue("SELECT $field FROM $tbName WHERE $condition");
		//var_dump($field);
		//return $field;
	}
	//lấy data có điều kiện
	
	
	//inser, update, delete
	function updateField($tbName, $set, $condition){		
		//mysql_query('SET NAMES utf8');
		$this->db->execute("UPDATE $tbName SET $set $condition");
		$message = $this->db->errorMes;
		//var_dump($flag);
		return $message; // NULL - > success
					  	 // #NULL -> fail
	}
	
	function deleteField($tbName, $condition){		
		//mysql_query('SET NAMES utf8');
		$this->db->execute("DELETE FROM $tbName WHERE $condition");
		$message = $this->db->errorMes; 
		//var_dump($flag);
		return $message; // NULL - > success
					  	 // #NULL -> fail
	}
	
	function insertNew($tbName,$columns,$val ){
		$this->db->execute("INSERT INTO $tbName $columns VALUES ($val)");
		//var_dump("INSERT INTO $tbName $columns VALUES ($val)");
		$message = $this->db->errorMes; 
		//var_dump($flag);
		return $message; // NULL - > success
					  	 // #NULL -> fail
	}
}

?>